package com.jy.novel.service.system;

import com.jy.novel.dao.system.RoleDao;
import com.jy.novel.dao.system.RoleMenuDao;
import com.jy.novel.dao.system.UserDao;
import com.jy.novel.pojo.system.RoleInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by yangqy on 4/29/16.
 */

@Service
public class RoleService {


    @Autowired
    RoleDao roleDao;
    @Autowired
    UserDao userDao;

    @Autowired
    RoleMenuDao roleMenuDao;



    /**
     * 分页查询角色列表
     * @param curPage
     * @param pageSize
     * @return
     */
    public List<RoleInfo> findRoleList(int curPage, int pageSize){
        return roleDao.findRoleList(curPage,pageSize);
    }

    public RoleInfo getRoleInfo(Long id) {
        return this.roleDao.getRoleInfo(id);
    }

    public boolean isExistRole(String name, String des) {
        return this.roleDao.isExistRole(name, des);
    }

    public boolean updateRoleInfo(Long id, String name, String remark) {
        return this.roleDao.updateRoleInfo(id, name, remark) > 0;
    }

    @Transactional
    public boolean deleteRole(Long sRoleId, Long dRoleId) {
        this.userDao.batChangeUserRole(sRoleId, dRoleId);
        this.roleDao.deleteRole(sRoleId);
        this.roleMenuDao.deleteRoleAllMenu(sRoleId);
        return true;
    }

    public boolean addRole(RoleInfo role) {
        return this.roleDao.addRole(role) > 0;
    }

    @Transactional
    public boolean cloneRole(long sRoleId, RoleInfo role) {
        Number roleId = this.roleDao.addRoleReturnId(role);
//        this.permissionDao.copyPermission(roleId.longValue(), sRoleId);
        return true;
    }

    public boolean isExistRole(Long roleId) {
        return this.roleDao.isExistRole(roleId);
    }
}

